package com.qcxy.gzy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.qcxy.gzy.activity.base.HiddenAdaptiveActivity;
import com.qxcy.gzy.util.SPUtils;

import java.util.Locale;

public class MainActivity extends HiddenAdaptiveActivity implements OnClickListener {

    private String card;
    private String scan;
    private String tymm;

    // Activity创建时被调用 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);
        LinearLayout mSetting = findViewById(R.id.ll_setting);
        LinearLayout mCardLogin = findViewById(R.id.ll_card);
        LinearLayout mSacnLogin = findViewById(R.id.ll_scan);
        LinearLayout mTymmLogin = findViewById(R.id.ll_visitor);

        card = SPUtils.getValueFromSharedFile(this, "card", "INVISIBLE");
        scan = SPUtils.getValueFromSharedFile(this, "scan", "INVISIBLE");
        tymm = SPUtils.getValueFromSharedFile(this, "tymm", "INVISIBLE");
        if (card.equals("VISIBLE")) {
            mCardLogin.setVisibility(View.VISIBLE);
        }
        if (scan.equals("VISIBLE")) {
            mSacnLogin.setVisibility(View.VISIBLE);
        }
        if (tymm.equals("VISIBLE")) {
            mTymmLogin.setVisibility(View.VISIBLE);
        }

        mSetting.setOnClickListener(this);
        mCardLogin.setOnClickListener(this);
        mSacnLogin.setOnClickListener(this);
        mTymmLogin.setOnClickListener(this);
    }

    // Activity创建或者从后台重新回到前台时被调用
    @Override
    protected void onStart() {
        super.onStart();
        card = SPUtils.getValueFromSharedFile(this, "card", "INVISIBLE");
        scan = SPUtils.getValueFromSharedFile(this, "scan", "INVISIBLE");
        tymm = SPUtils.getValueFromSharedFile(this, "tymm", "INVISIBLE");
    }

    // Activity从后台重新回到前台时被调用
    @Override
    protected void onRestart() {
        super.onRestart();
        card = SPUtils.getValueFromSharedFile(this, "card", "INVISIBLE");
        scan = SPUtils.getValueFromSharedFile(this, "scan", "INVISIBLE");
        tymm = SPUtils.getValueFromSharedFile(this, "tymm", "INVISIBLE");
    }

    // Activity创建或者从被覆盖、后台重新回到前台时被调用
    @Override
    protected void onResume() {
        super.onResume();
        card = SPUtils.getValueFromSharedFile(this, "card", "INVISIBLE");
        scan = SPUtils.getValueFromSharedFile(this, "scan", "INVISIBLE");
        tymm = SPUtils.getValueFromSharedFile(this, "tymm", "INVISIBLE");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_setting:
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
                break;
            case R.id.ll_scan:
                intent = new Intent(MainActivity.this, ScanActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_card:
                intent = new Intent(MainActivity.this, CardActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_visitor:
                //v2
                intent = new Intent(MainActivity.this, FirstPageActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public static void showLanguage(Context context) {
        // 默认简体中文
        String language = SPUtils.getValueFromSharedFile(context, "LANGUAGE_KEY", "zh-CN");
        // 设置应用语言类型
        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        if (language.equals("zh-CN")) {// 简体中文
            config.locale = Locale.ENGLISH;
        } else if (language.equals("en")) {// 英文
            config.locale = Locale.SIMPLIFIED_CHINESE;
        }
        resources.updateConfiguration(config, dm);
    }
}